﻿
param(
	[string]$installationUrl = $(throw "Please specify the url of your SharePoint Mysite Host Site Collection")
)
"Install Invoked..."
 
##### DEFINE VARIABLES FOR SCRIPT #####
	
	$SiteUrl = $installationUrl
	#$SiteUrl = "http://st-egov-dev01:2551/my"

	$SolutionName = "eGov.SharePoint.MyPeeps.wsp"
	$SolutionPath = ".\eGov.SharePoint.MyPeeps.wsp"
	$MySiteFeatureID = "cd8bf2b6-5a4c-4ff8-bb87-f938271a52ed"
	$MySiteHostFeatureID = "5e311e70-08dd-43d5-9f12-fcc9b2f7f0cd"
	$PublishingWebFeatureID = "F6924D36-2FA8-4f0b-B16D-06B7250180FA" 
	$PublishingSiteFeatureID = "94C94CA6-B32F-4da9-A9E3-1F3D343D7ECB"


##### BEGIN DEPLOYMENT SCRIPT #####

	# Ensure Microsoft.SharePoint.PowerShell is loaded
	$snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell’}
	if ($snapin -eq $null)
	{
		"Loading SharePoint Powershell”
		Add-PSSnapin "Microsoft.SharePoint.Powershell”
	}

Try
{
        
	$site = Get-SPSite $SiteUrl
	$MySiteWebApplication = $Site.WebApplication
	$SiteCollection = $MySiteWebApplication.Sites

	# Check if Solution Exists
	$isSolutionInStore = $null	
    	$WSP = Get-SPSolution | Where {($SolutionName -eq $_.Name)}      
     	if($WSP -eq $null) 
    	{ 	
		$isSolutionInStore = 0 
	}  
    else 
	{ 
		$isSolutionInStore = 1 
		.\Uninstall.ps1 $SiteUrl 
	} 

##### Installing existing Solution Package ######
	"*** Adding new solution package"
	[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath

	Add-SPSolution -LiteralPath $SolutionPath -confirm:$false

	"*** Installing solution package"
	Install-SPSolution -Identity $SolutionName -Local -GACDeployment -force -WebApplication $MySiteWebApplication 
	"*** DONE Installing Solution"

	$WSP = Get-SPSolution | Where {($SolutionName -eq $_.Name)}      
    	if($WSP -eq $null) 
    	{ 	
		throw "SOLUTION DID NOT INSTALL CORRECTLY; Checking solution existance in the solution store after install returned Null"
	}


	##### Feature Activation ######
	"*** Starting Feature Activation"


	$RootWeb = $site.OpenWeb("")

	"*** Activating eGov MyPeeps MySite 'Host' Customization"
	if($RootWeb.WebTemplate -eq "SPSMSITEHOST")
	{
		#Activate MySite Host Feature
		"   * Activating MySite 'Host' Customization (eGov MyPeeps) Feature on $SiteUrl "
		Enable-SPFeature -url $SiteUrl -Identity $MySiteHostFeatureID -force
		"   * Activated"

		$Feature  = Get-SPFeature -Identity $MySiteHostFeatureID -site $site
		if($Feature -eq $null)
		{
			"###ERROR# Could not activate  MySite Host Feature on $SiteUrl"
		}
	}

	"*** Activating eGov MyPeeps MySite Customization on existing user mysites "
	for ($i=0; $i -lt $SiteCollection.Count; $i++ )
	{
		$RootWeb = $SiteCollection[$i].OpenWeb("")
	
		if($RootWeb.WebTemplate -eq "SPSPERS")
		{
			$RootWebUrl = $RootWeb.Url
			"   * Activating eGov MyPeeps MySite Customization Feature on $RootWebUrl "
			Enable-SPFeature -url $RootWebUrl -Identity $MySiteFeatureID -force			
			
			$Feature  = Get-SPFeature -Identity $MySiteFeatureID -Site $SiteCollection[$i]
			if($Feature -eq $null)
			{
				"   ### ERROR ### Could not activate Feature on  a user MySite collection: $RootWebUrl" 
			}
			else
			{
				"   * Activated"
			}
		}

		$RootWeb.Dispose()
	}


	"*** Restarting IIS"
	iisreset
	"*** Done Activating Features"

	#"*** Setting Activity Streams Timer Job to run every 1 minute"
	#$tj =  Get-SPTimerJob -Identity Users_ActivityFeedJob
	#Enable-SPTimerJob $tj
	#Set-SPTimerJob $tj -Schedule "every 1 minutes between 0 and 59”


	"*** Installation Completed Successfully"
	

	}
	Catch
	{
		"### FATAL ERROR ###  - Reversing Installation by running the uninstall script - Error discription will be thrown as a fatal exception after uninstallation"
		.\Uninstall.ps1 $SiteUrl 
		throw new-object Exception("Solution Installation ERROR", $error[0].Exception)
	}

